<ol class="breadcrumb s-breadcrumb" style="">
    <li><a href="/" class="glyphicon glyphicon-home"></a></li>
    <li><a href="/codelab">Codelab</a></li>
    <li><a href="#">源码安装Yii2.0框架</a></li>
</ol>

<div class="container-fluid">
    <div class="row">
        <div class="col-lg-2 col-md-3 col-sm-3 hidden-xs">
            <div class="s-sidebar">
                <div class="step s-active">
                    <span class="s-num">1</span>
                    <span class="s-text">准备工作</span>
                </div>
                <div class="step">
                    <span class="s-num">2</span>
                    <span class="s-text">安装PHP</span>
                </div>
                <div class="step">
                    <span class="s-num">3</span>
                    <span class="s-text">下载Yii</span>
                </div>
                <div class="step">
                    <span class="s-num">4</span>
                    <span class="s-text">配置Nginx</span>
                </div>
                <div class="step">
                    <span class="s-num">5</span>
                    <span class="s-text">完成安装</span>
                </div>
            </div>

        </div>
        <div class="col-lg-10 col-md-9 col-sm-9">
            <article>
                <div>
                    <div class="s-content" aria-level="step1">
                        <h1>源码安装Yii2.0框架</h1>
                        <div class="s-body">
                            <h2>1、准备工作</h2>
                            <p>安装Yii2.0框架需要一个可以运行Linux系统的电脑，可以是云服务器或者本地的Linux虚拟机。虽然PHP也支持在windows电脑下运行，但是要知道几乎所有的服务器都是Linux系统的，只有在开发时使用和生产环境同样的操作系统才能在开发时发现并解决一些和操作系统相关的问题。</p>
                            <h3>Ubuntu Server</h3>
                            <p>由于CentOS已经停止运维更新了，因此建议安装<a href="http://mirrors.163.com/ubuntu-releases/21.10/ubuntu-21.10-live-server-amd64.iso">Ubuntu Server</a>虚拟机作为本地开发环境。</p>
                            <h3>Docker</h3>
                            <p>如果你安装了Docker，又想学习如何基于源码安装Yii，那可以直接拉取ubuntu镜像而不用刻意安装Linux虚拟机。</p>
                            <?=\frontend\modules\yii2\widgets\layouts\markdown\Markdown::widget(["content" => <<<CODE
```shell
docker run -itd --name lnmp -p 80:80 ubuntu
```
CODE
                            ])?>
                        </div>
                    </div>
                    <div class="s-content" aria-level="step2">
                        <div class="s-body">
                            <h2>2. 安装PHP</h2>
                            <p>建议采用LNMP脚本安装PHP。</p>
                            <h3>LNMP</h3>
                            <p><a href="https://lnmp.org/" title="LNMP官网链接地址">LNMP</a>是基于Linux的一个安装php/mysql/nginx/apache/ftp的脚本工具，实现了针对不同Linux系统实现一键安装的功能，用户只需要在启动LNMP的时候选择合适的php/mysql/nginx版本无值守安装，简单快捷。</p>
                            <p>在shell中执行如下命令：</p>
                            <?=\frontend\modules\yii2\widgets\layouts\markdown\Markdown::widget(["content" => <<<CODE
```shell
wget http://soft.vpser.net/lnmp/lnmp1.8.tar.gz -cO lnmp1.8.tar.gz && \
tar zxf lnmp1.8.tar.gz && \
cd lnmp1.8 && \
./install.sh lnmp
```
CODE
                            ])?>
                            <p>按照自己的要求选择需要安装的软件版本，我们这里选择Mysql8.0，PHP8.0：</p>
                            <?=\frontend\modules\yii2\widgets\layouts\markdown\Markdown::widget(["content" => <<<CODE
```shell
+------------------------------------------------------------------------+
|          LNMP V1.8 for Ubuntu Linux Server, Written by Licess          |
+------------------------------------------------------------------------+
|        A tool to auto-compile & install LNMP/LNMPA/LAMP on Linux       |
+------------------------------------------------------------------------+
|           For more information please visit https://lnmp.org           |
+------------------------------------------------------------------------+
You have 11 options for your DataBase install.
1: Install MySQL 5.1.73
2: Install MySQL 5.5.62 (Default)
3: Install MySQL 5.6.51
4: Install MySQL 5.7.34
5: Install MySQL 8.0.25
6: Install MariaDB 5.5.68
7: Install MariaDB 10.1.48
8: Install MariaDB 10.2.38
9: Install MariaDB 10.3.29
10: Install MariaDB 10.4.19
0: DO NOT Install MySQL/MariaDB
Enter your choice (1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or 0): 5
You will install MySQL 8.0.25
===========================
Please setup root password of MySQL.
Please enter: lv_123456                     
MySQL root password: lv_123456
===========================
Do you want to enable or disable the InnoDB Storage Engine?
Default enable,Enter your choice [Y/n]: Y
You will enable the InnoDB Storage Engine
===========================
You have 9 options for your PHP install.
1: Install PHP 5.2.17
2: Install PHP 5.3.29
3: Install PHP 5.4.45
4: Install PHP 5.5.38
5: Install PHP 5.6.40 (Default)
6: Install PHP 7.0.33
7: Install PHP 7.1.33
8: Install PHP 7.2.34
9: Install PHP 7.3.29
10: Install PHP 7.4.21
11: Install PHP 8.0.8
Enter your choice (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11): 11
You will install PHP 8.0.8
===========================
You have 3 options for your Memory Allocator install.
1: Don't install Memory Allocator. (Default)
2: Install Jemalloc
3: Install TCMalloc
Enter your choice (1, 2 or 3): 1
You will install not install Memory Allocator.

Press any key to install...or Press Ctrl+c to cancel

```
CODE
                            ])?>
                            <p>如果出现如下输出，则表示安装成功：</p>
                            <?=\frontend\modules\yii2\widgets\layouts\markdown\Markdown::widget(["content" => <<<CODE
```shell
+-------------------------------------------+
|    Manager for LNMP, Written by Licess    |
+-------------------------------------------+
|              https://lnmp.org             |
+-------------------------------------------+
nginx (pid 22993) is running...
php-fpm is runing!
 * MySQL running (23524)
include/end.sh: line 248: netstat: command not found
Install lnmp takes 164 minutes.
Install lnmp V1.8 completed! enjoy it.
```
CODE
                            ])?>
                            <p>LNMP的详细信息可以查看如下文件：</p>
                            <?=\frontend\modules\yii2\widgets\layouts\markdown\Markdown::widget(["content" => <<<CODE
```shell
root:/# tail -22 /lnmp1.8/README 
#目录位置
* Nginx：/usr/local/nginx/
* MySQL：/usr/local/mysql/
* MariaDB：/usr/local/mariadb/
* PHP：/usr/local/php/
* 多PHP目录：/usr/local/php5.6/ 版本号随安装版本不同而不同
* PHP扩展插件配置文件目录：/usr/local/php/conf.d/
* PHPMyAdmin：/home/wwwroot/default/phpmyadmin/
* 默认虚拟主机网站目录：/home/wwwroot/default/
* Nginx日志目录：/home/wwwlogs/

#配置文件：
* Nginx主配置文件：/usr/local/nginx/conf/nginx.conf
* MySQL/MariaDB配置文件：/etc/my.cnf
* PHP配置文件：/usr/local/php/etc/php.ini
* PHP-FPM配置文件：/usr/local/php/etc/php-fpm.conf
* PureFtpd配置文件：/usr/local/pureftpd/etc/pure-ftpd.conf
* Apache配置文件：/usr/local/apache/conf/httpd.conf

技术支持

技术支持论坛：https://bbs.vpser.net/forum-25-1.html
```
CODE
                            ])?>
                        </div>
                    </div>
                    <div class="s-content" aria-level="step3">
                        <div class="s-body">
                            <?=\frontend\modules\yii2\widgets\layouts\markdown\Markdown::widget(["content" => <<<CODE
## 3. 下载Yii
LNMP的项目文件存放目录是`/home/wwwroot/`，因此你需要把Yii源码放到此目录下。
```shell
cd /home/wwwroot
git clone -b 2.0.43 https://github.com/yiisoft/yii2-app-advanced.git yii
```

### 3.1 下载依赖包
下载的归档文件并没有`vendor`依赖包目录，无法运行Yii，因此我们需要`composer`依赖管理工具来拉取Yii的依赖。`composer`下载如下：
```shell
cd /home/wwwroot/yii
curl -sS https://getcomposer.org/installer | php
```
上述操作已经把`composer`下载到了当前项目的根目录下，运行如下命令拉取依赖：
```shell
php composer.phar update
```
下载依赖包时间会久一点，这取决于您的网络情况，请耐心等待。

### 3.2 初始化Yii
现在依然无法运行Yii，因为没有入口文件和配置文件，需要手动运行根目录下的`init`脚本创建这些必要的文件：
```shell
root@1955048abc77:/home/wwwroot/yii# ./init
Yii Application Initialization Tool v1.0

Which environment do you want the application to be initialized in?

  [0] Development
  [1] Production

  Your choice [0-1, or "q" to quit] 0

  Initialize the application under 'Development' environment? [yes|no] yes

  Start initialization ...

   generate console/config/main-local.php
   generate console/config/params-local.php
   generate console/config/test-local.php
   generate frontend/web/index.php
   generate frontend/web/index-test.php
   generate frontend/web/robots.txt
   generate frontend/config/main-local.php
   generate frontend/config/codeception-local.php
   generate frontend/config/params-local.php
   generate frontend/config/test-local.php
   generate common/config/main-local.php
   generate common/config/codeception-local.php
   generate common/config/params-local.php
   generate common/config/test-local.php
   generate yii_test.bat
   generate backend/web/index.php
   generate backend/web/index-test.php
   generate backend/web/robots.txt
   generate backend/config/main-local.php
   generate backend/config/codeception-local.php
   generate backend/config/params-local.php
   generate backend/config/test-local.php
   generate yii_test
   generate yii
   generate cookie validation key in backend/config/main-local.php
   generate cookie validation key in common/config/codeception-local.php
   generate cookie validation key in frontend/config/main-local.php
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 console/runtime
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0755 yii
      chmod 0755 yii_test

  ... initialization completed.
```
CODE
                            ])?>
                        </div>
                    </div>
                    <div class="s-content" aria-level="step4">
                        <div class="s-body">
                            <?=\frontend\modules\yii2\widgets\layouts\markdown\Markdown::widget(["content" => <<<CODE
## 4. 配置Nginx 
到目前为止我们的Yii框架代码已经完整，但是如果想让浏览器可以访问到Yii，还需要配置服务器代理，我们用LNMP默认安装的是`Nginx`，因此我们介绍如何基于`Nginx`完成Yii的服务代理配置。
### 4.1 修改`fastcgi.conf`文件
`LNMP`的`fastcgi.conf`配置不支持yii，因此需要修改一下，在命令行中运行如下命令即可：
```shell
sed -i 's/\$document_root\/\:\/tmp\/\:\/proc\//NULL/g' /usr/local/nginx/conf/fastcgi.conf
```

### 4.2 创建yii.conf文件
下面的代码就是需要创建的配置文件，文件路径为：`/usr/local/nginx/conf/vhost/yii.conf`：
```shell
server {
    charset utf-8;
    client_max_body_size 128M;

    listen 8080; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name test.yii;
    root        /home/wwwroot/yii/frontend/web;
    index       index.php;

    #remove the index.php in url
    location / {
        # Redirect everything that isn't a real file to index.php
        try_files \$uri \$uri/ /index.php\$is_args\$args;
    }

    #send the request to php
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
        #fastcgi_pass 127.0.0.1:9000;
        fastcgi_pass unix:/tmp/php-cgi.sock;
        try_files \$uri =404;
    }

    access_log  /home/wwwlogs/yii_access.log;
    error_log   /home/wwwlogs/yii_error.log;
}
```

### 4.3 重启Nginx
最后不要忘记重启Nginx，命令如下：
```shell
nginx -s reload
```
CODE
                            ])?>
                        </div>
                    </div>
                    <div class="s-content" aria-level="step5">
                        <div class="s-body">
                            <?=\frontend\modules\yii2\widgets\layouts\markdown\Markdown::widget(["content" => <<<CODE
## 5. 完成安装
现在已经完成了代理服务器的配置，可以通过浏览器访问我们的Yii2.0框架了，找到虚拟机的IP，在浏览器访问`8080`端口即可，如果出现如下的页面，就表示您已经完成了Yii2.0的安装：
![Yii欢迎页面](/images/20211109180257.png)
CODE
                            ])?>
                        </div>
                    </div>
                </div>

                <button class="btn btn-default back">返回</button>
                <button class="btn btn-primary next">下一步</button>
            </article>
            <footer class="footer vertical-container">
                <p><span>浙ICP备2021007911号</span>Powered by @陈致威</p>
            </footer>
        </div>
    </div>
</div>


